layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/team/user/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
       {field: 'id', title: '用户ID',width:80}
      ,{field: 'name', title: '用户名称'}
      ,{field: 'phone', title: '用户手机号'}
      ,{field: 'nickname', title: '昵称'}
      ,{field: 'sexName', title: '性别',width:80}
	  ,{field: 'roleTypeName', title: '角色',width:100}
      ,{field: 'placeCount', title: '管理网点',width:100}
	  ,{field: 'deviceCount', title: '设备数量',width:100}
	  ,{field: 'funcModelCount', title: '功能模块',width:100}
      ,{field: 'note', title: '备注'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 180, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
      add:function (){
        layer.open({
            type: 2
            , title: '新增用户'
            , content: layui.setter.webRoot + '/team/user/add.html'
            , maxmin: true
            , area: ['500px', '600px']
            ,btn: ['确定', '取消']
            , yes: function(index, layero){
                var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-form-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);
                iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                    var field = data.field;
                    $.ajax({
                        type: "post",
                        url: layui.setter.webRoot + '/team/user/add.do',
                        data:field,
                        dataType:"json",
                        success:function(res){
                            if(res.code == 200){
                                table.reload('LAY-table');
                                layer.close(index);
                            }else{
                                layer.msg(res.msg,{icon: 2});
                            }
                        }
                    });
                });
                submit.trigger('click');
            }
        })
      }
      ,info:function(obj){
          layer.open({
              type: 2
              ,title: '用户详情'
              ,content: layui.setter.webRoot + '/team/user/info.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
      }
      ,walletInfo:function(obj){
          layer.open({
              type: 2
              ,title: '用户钱包'
              ,content: layui.setter.webRoot + '/team/user/wallet/info.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
      }
    ,del:function(obj){
    	layer.confirm('确定要删除该用户信息吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/team/user/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('user_list', {})
});